//
//  SEGDoorListViewModel.m
//  Commom
//
//  Created by Clavis on 2020/6/9.
//

#import "SEGDoorListViewModel.h"
#import <SEGModel/SEGNewDoorManager.h>
#define SEGDoorComponentMaxNumber 8 //门禁组件最多个数

@interface SEGDoorListViewModel ()

@property (nonatomic, strong) SEGNewDoorManager *doorManager;
@property (nonatomic, strong) RACSignal *doorListSignal;

@end

@implementation SEGDoorListViewModel

/// 提交开门记录（蓝牙门）
- (void)submitOpenDoorHistory {
    [self.doorManager submitOpenDoorHistory];
}

/// 获取本地门禁列表
- (NSArray<SEGNewMyDoorListModel *> *)getLocalAllDoorList {
    [self.doorManager getAllDoorList];
    return [self.doorManager.dataMyDoorListSource copy];
}

/// 获取网络门禁列表
- (RACSignal *)requestNetAllDoorList {
    return self.doorListSignal;
}

/// 保存排序之后的门禁列表
/// @param sortDoorList sortDoorList
- (void)saveSortDoorList:(NSArray<SEGNewMyDoorListModel *> *)sortDoorList {
    [self.doorManager saveSortDoor:sortDoorList];
}

#pragma mark - 门禁组件
/// 获取门禁组件数据（先返回本地数据 再返回网络数据)
/// @param completedBlock completedBlock
- (void)requestDoorComponentList:(void(^)(NSArray<NSDictionary*>*))completedBlock {
    // 门禁组件只显示前八个门禁
    // 先返回本地数据
    NSArray<NSDictionary*>* doorCommoentList = [[[self getLocalAllDoorList].rac_sequence take:SEGDoorComponentMaxNumber] map:^id _Nullable(SEGNewMyDoorListModel * _Nullable model) {
        return [model yy_modelToJSONObject];
    }].array;
    if (completedBlock) {
        completedBlock(doorCommoentList);
    }
    
    // 再返回网络数据
    [self.doorListSignal subscribeNext:^(NSArray<SEGNewMyDoorListModel *> *  _Nullable doorList) {
        // 门禁组件只显示前八个门禁
        NSArray<NSDictionary*>* doorCommoentList = [[doorList.rac_sequence take:SEGDoorComponentMaxNumber] map:^id _Nullable(SEGNewMyDoorListModel * _Nullable model) {
            return [model yy_modelToJSONObject];
        }].array;
        if (completedBlock) {
            completedBlock(doorCommoentList);
        }
    }];
}

#pragma mark - handle CommonDoor
/// 获取老的常用门
- (NSArray<SEGNewMyDoorListModel*> *)getOldCommonDoor {
    return [[self.doorManager getCommonDoor] copy];
}

// 更新老常用门
- (void)updateOldCommonDoors:(NSArray<SEGNewMyDoorListModel *> *)newCommonDoors {
    [self.doorManager updateCommonDoors:newCommonDoors];
}

#pragma mark - handle OneQrcodePass door
/// 获取一码通行门禁
- (SEGNewMyDoorListModel*)getOneQrcodePassDoor {
    NSArray<SEGNewMyDoorListModel*> *localDoorArray = [self getLocalAllDoorList];
    SEGNewMyDoorListModel *haikangDoor = [[localDoorArray.rac_sequence filter:^BOOL(SEGNewMyDoorListModel * _Nullable doorModel) {
        return [doorModel.deviceType integerValue] == SEGDoorDeviceType_HaiKang;
    }] take:1].array.firstObject;
    return haikangDoor;
}

/// 获取除一码通行之外的门禁列表
- (NSArray<SEGNewMyDoorListModel*> *)getLocalAllDoorListExceptOneQrcodePassDoor {
    NSArray<SEGNewMyDoorListModel*> *localDoorArray = [self getLocalAllDoorList];
    return [localDoorArray.rac_sequence filter:^BOOL(SEGNewMyDoorListModel * _Nullable doorModel) {
        return [doorModel.deviceType integerValue] != SEGDoorDeviceType_HaiKang;
    }].array;
}

#pragma mark - Getter
- (RACSignal *)doorListSignal {
    if (!_doorListSignal) {
        @weakify(self);
        _doorListSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            [self.doorManager requestmyDoorListFinished:^(BOOL isSuccessed, NSString *codeType, NSString *errorMessage) {
                //网络门禁列表请求成功之后会存本地，所以返回本地列表
                [subscriber sendNext:[self getLocalAllDoorList]];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
    }
    return _doorListSignal;
}


- (SEGNewDoorManager *)doorManager {
    if (!_doorManager) {
        _doorManager = [[SEGNewDoorManager alloc] init];
    }
    return _doorManager;
}

@end
